<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('getPagination')){
	function getPagination($input){
		$result = array();
        if(!isset($input['start']) || !validate($input['start'])){
            $result['start'] = LIST_DEFAULT_START;
		}else{
			$result['start'] = intval($input['start']);
		}

        if(!isset($input['limit']) || !validate($input['limit'])){
            $result['limit'] = LIST_DEFAULT_LIMIT;
		}else{
			$result['limit'] = intval($input['limit']);
		}
        
        return $result;
	}
}

if ( ! function_exists('compressHTML')){
	function compressHTML($buffer){
		$search = array(
		'/\>[^\S ]+/s',
		'/[^\S ]+\</s',
		'/(\s)+/s'
		);
		$replace = array(
		'>',
		'<',
		'\\1'
		);
		if (preg_match("/\<html/i",$buffer) == 1 && preg_match("/\<\/html\>/i",$buffer) == 1) {
		$buffer = preg_replace($search, $replace, $buffer);
		}
		return $buffer;
	}
}

if ( ! function_exists('getFilter')){
	function getFilter($input,$filterList){
		
		$filter = array();
		if(is_array($input) && is_array($filterList)){
			foreach($input as $key => $value){
				if(array_key_exists($key,$filterList) && $value != ''){
					$filter[$filterList[$key]] = $value;
				}
			}
		}

		return $filter;
	}
}

if ( ! function_exists('prepare_file_name')){
	function prepare_file_name(&$filename){
		$filename_replace = array('\\','/',':','*','?','"','\'','<','>','|');

		foreach($filename_replace as $token){
			$filename = str_replace($token,'_',$filename);
		}
	}
}

if ( ! function_exists('remove_javascript')){
	function remove_javascript(&$content){
		while(true){
			$start = strpos($content,'<script');
			$end = strpos($content,'</script>');
			if($start === false || $end === false) break;
			$content = substr($content,0,$start).substr($content,$end+9);
		}
	}
}

if ( ! function_exists('remove_css')){
	function remove_css(&$content){
		while(true){
			$start = strpos($content,'<style');
			$end = strpos($content,'</style>');
			if($start === false || $end === false) break;
			$content = substr($content,0,$start).substr($content,$end+8);
		}
	}
}

if ( ! function_exists('show')){
	function show($content,$flg_die = true){
		echo '<pre>';
		if(is_array($content)) print_r($content);
		else var_dump($content);
		echo '</pre>';
		if($flg_die) die();
	}
}

if ( ! function_exists('getSort')){
	function getSort($input,$sortList){
		$sort = array();
		if(is_array($input) && isset($input['sort']) && isset($input['dir']) && is_array($sortList)){
			if(isset($sortList[$input['sort']])){
				$sort['key'] = $sortList[$input['sort']];
				$input['dir'] = ($input['dir']=='DESC')?'desc':'asc';
				$sort['dir'] = $input['dir'];
			}
		}

		return $sort;
	}
}

if ( ! function_exists('seoUrl')){
	function seoUrl($string) {
		//Lower case everything
		$string = strtolower($string);
		//Make alphanumeric (removes all other characters)
		$string = preg_replace("/[^a-z0-9_\s-]/", "", $string);
		//Clean up multiple dashes or whitespaces
		$string = preg_replace("/[\s-]+/", " ", $string);
		//Convert whitespaces and underscore to dash
		$string = preg_replace("/[\s_]/", "-", $string);
		return $string;
	}
}

if ( ! function_exists('date_diff')){
	function date_diff($date1, $date2) { 
	    $current = $date1; 
	    $datetime2 = date_create($date2); 
	    $count = 0; 
	    while(date_create($current) < $datetime2){ 
	        $current = gmdate("Y-m-d", strtotime("+1 day", strtotime($current))); 
	        $count++; 
	    } 
	    return $count; 
	} 
}

if ( ! function_exists('randString')){

	function randString($num=5) {
	    $re = '';
	    $s = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	    while(strlen($re)<$num) {
	        $re .= $s[rand(0, strlen($s)-1)]; //从$s中随机产生一个字符
	    }
	    return $re;
	}
}

if(!function_exists('id2name')) {
	function id2name($key,$arr) {
		if(isset($arr[$key])) {
			return $arr[$key];
		} 
		return '';
	}
}

function remove_emoji($text){
  return preg_replace('/\xEE[\x80-\xBF][\x80-\xBF]|\xEF[\x81-\x83][\x80-\xBF]/', '', $text);
}
